package com.android.demo_20220829.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.view.WindowManager;

import com.android.baselibrary.interface_.CommCallBack;
import com.android.baselibrary.tool.SPUtil;
import com.android.demo_20220829.R;
import com.android.demo_20220829.constants.SPConstants;
import com.android.demo_20220829.dialog.XieYi_DialogFragment;
import com.android.demo_20220829.manager.UserManager;
import com.gyf.immersionbar.ImmersionBar;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2018/7/21.
 */

public class LoadingActivity extends BaseActivity {

    Handler handler = new Handler();
    Context mContext;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_loading;
    }

    @Override
    protected void initView() {
        ImmersionBar.with(this).statusBarDarkFont(true).navigationBarDarkIcon(true).init();
        mContext = this;
        setContentView(R.layout.activity_loading);
        boolean hasOk = SPUtil.getBoolValue(mContext, SPConstants.HasAccept, true);
        if (hasOk) {
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    goToMain();
                }
            }, 1500);
        } else {
            XieYi_DialogFragment dialogFragment = (XieYi_DialogFragment) XieYi_DialogFragment.showDialog(getSupportFragmentManager(), null);
            dialogFragment.setCallback(new CommCallBack() {
                @Override
                public void onResult(Object obj) {
                    boolean result = (boolean) obj;
                    if (!result) {
                        Map<String, Object> params = new HashMap<>();
                        params.put("isSecond", true);
                        XieYi_DialogFragment dialogFragment1 = (XieYi_DialogFragment) XieYi_DialogFragment.showDialog(getSupportFragmentManager(), params);
                        dialogFragment1.setCallback(new CommCallBack() {
                            @Override
                            public void onResult(Object obj) {
                                boolean result = (boolean) obj;
                                if (result) {
                                    SPUtil.putValue(mContext, SPConstants.HasAccept, true);
                                    goToMain();
                                }
                            }
                        });
                    }
                    else
                    {
                        SPUtil.putValue(mContext, SPConstants.HasAccept, true);
                        goToMain();
                    }
                }
            });
        }
    }

    @Override
    protected void initData() {
    }

    //跳首页
    private void goToMain() {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN, WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
        Intent intent;
        if (UserManager.isLogin(mContext)) {
            intent = new Intent(mContext, MainActivity.class);
        } else {
            intent = new Intent(mContext, LoginActivity.class);
        }
        intent = new Intent(mContext, MainActivity.class);
        mContext.startActivity(intent);
        overridePendingTransition(R.anim.main_activity_in, R.anim.splash_out);
        finish();
    }


}
